package com.haojiang.mh.remote.utils

import android.app.Activity
import android.app.AlertDialog
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.media.SoundPool
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.widget.loading.view.LoadingDialog


class Util {

    companion object {

//        private var soundPool: SoundPool? = null//音频通知声音播放器
//        private var soundID = 0 //音频资源ID

        private var loadingDialog: LoadingDialog? = null
//        private var rxDialogShapeLoading: RxDialogShapeLoading? = null


        fun showLoading(context: Context, msg: String?) {
            if (loadingDialog == null) {
                loadingDialog = LoadingDialog(context)
                loadingDialog?.setLoadingText(msg) //                    .setFailedText(null)
                    //                    .setInterceptBack(true)
                    ?.setLoadSpeed(LoadingDialog.Speed.SPEED_TWO) //                    .setRepeatCount(0)
                    //                        .setDrawColor(color)
                    //                    .setShowTime(0)//延时5秒自动关闭，默认1秒
                    ?.setLoadStyle(LoadingDialog.STYLE_RING)
            }
            loadingDialog?.show()
//            ThreadUtils.ui {
//                if(rxDialogShapeLoading == null) {
//                    rxDialogShapeLoading = RxDialogShapeLoading(context)
//                }
//                rxDialogShapeLoading!!.show()
//            }
        }

        fun hideLoading() {
            ThreadUtils.ui(Runnable {
                if (loadingDialog != null) {
                    loadingDialog!!.close()
                    loadingDialog = null
                }
//                if(rxDialogShapeLoading != null) {
//                    rxDialogShapeLoading?.dismiss()
//                    rxDialogShapeLoading = null
//                }
            })
        }

        //检查蓝牙是否支持及打开
        fun checkBleStatus(context: Context): Boolean {
            val defaultAdapter = BluetoothAdapter.getDefaultAdapter()
            if (defaultAdapter == null ||
                !context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)
            ) {
                Toast.makeText(context, R.string.m_ble_not_supported, Toast.LENGTH_SHORT).show()
                (context as Activity).finish()
            }
            if (!defaultAdapter.isEnabled) {
                return defaultAdapter.enable()
            } else {
                return true
            }
        }


        //获取屏幕宽度、高度
        fun getScreenWidth(context: Context): Int {
            val windowManager =
                context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val dm = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(dm)
            return dm.widthPixels
        }

        fun getScreenHeight(context: Context): Int {
            val windowManager =
                context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val dm = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(dm)
            return dm.heightPixels
        }

        fun dip2px(context: Context, dipValue: Float): Int {
            val displayMetrics = context.resources.displayMetrics
            val scale = displayMetrics.density
            return (dipValue * scale + 0.5f).toInt()
        }


        fun shake(context: Context, ms: Long) {
            if (Build.VERSION.SDK_INT >= 26) {
                (context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator).vibrate(
                    VibrationEffect.createOneShot(ms, 1)
                )
            } else {
                (context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator).vibrate(
                    ms
                )
            }
        }

        /**
         * 版本名(自定义) 1.2.2
         * [获取应用程序版本名称信息]
         *
         * @param
         * @return 当前应用的版本名称
         */
        fun getVersionName(): String? {
            try {
                val packageManager: PackageManager = AppContext.instance().getPackageManager()
                val packageInfo: PackageInfo = packageManager.getPackageInfo(
                    AppContext.instance().getPackageName(), 0
                )
                return packageInfo.versionName
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return null
        }


        /**
         * 版本名(系统) 162
         * [获取应用程序版本名称信息]
         *
         * @param
         * @return 当前应用的版本名称
         */
        fun getVersionCode(): Int {
            try {
                val packageManager: PackageManager = AppContext.instance().getPackageManager()
                val packageInfo: PackageInfo = packageManager.getPackageInfo(
                    AppContext.instance().getPackageName(), 0
                )
                return packageInfo.versionCode
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return 0
        }


        /**
         * 获取包名例如：com.richmat.rmcontrol
         * [获取应用程序版本名称信息]
         *
         * @param
         * @return 当前应用的版本名称
         */
        fun getPackageName(): String? {
            try {
                val packageManager: PackageManager = AppContext.instance().getPackageManager()
                val packageInfo: PackageInfo = packageManager.getPackageInfo(
                    AppContext.instance().getPackageName(), 0
                )
                return packageInfo.packageName
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return null
        }

        /**
         * 显示提示框
         *
         * @param context
         * @param msg
         */
        fun showDialog(context: Context, msg: String?) {
            ThreadUtils.ui {
                AlertDialog.Builder(context)
                    .setMessage(msg)
                    .setPositiveButton(
                        AppContext.instance().getString(R.string.f_confirm)
                    ) { dialog, which -> dialog.dismiss() }
                    .show()
            }
        }


        fun clickItemDelay(view: View, delayMs: Int) {
            view.isEnabled = false
            view.postDelayed({ view.isEnabled = true }, delayMs.toLong())
        }

        /**
         * 隐藏键盘
         * 弹窗弹出的时候把键盘隐藏掉
         */
        fun hideInputKeyboard(activity: Activity) {
            val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(activity.window.decorView.windowToken, 0)
        }

        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        fun initSound() {
//            soundPool = SoundPool.Builder().build();//实例化音频通知播放器
//            soundID = soundPool!!.load(AppContext.instance(), R.raw.click, 1);//设置资源ID
        }

        fun playSound() {
//            soundPool?.let {
//                soundPool!!.play(
//                    soundID,
//                    0.1f,      //左耳道音量【0~1】
//                    0.5f,      //右耳道音量【0~1】
//                    0,         //播放优先级【0表示最低优先级】
//                    0,         //循环模式【0表示循环一次，-1表示一直循环，其他表示数字+1表示当前数字对应的循环次数】
//                    1F          //播放速度【1是正常，范围从0~2】
//                )
//            }
        }

        /**
         * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
         */
//        fun dip2px(context: Context, dpValue: Float): Int {
//            val scale = context.resources.displayMetrics.density
//            return (dpValue * scale + 0.5f).toInt()
//        }

    }


}